package ua.kpi.flats.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ua.kpi.flats.entity.Meeting;
import ua.kpi.flats.entity.SaleApplication;
import ua.kpi.flats.entity.SuitableOption;
import ua.kpi.flats.service.ApplicationService;
import ua.kpi.flats.service.MeetingService;

@Controller
public class RealtorController {
	private ApplicationService applicationService;
	private MeetingService meetingService;
	
	@RequestMapping(value = "/realtor/get_applications.do", method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> getApplications(@RequestParam("start") int from,
			                                                @RequestParam("limit") int limit) {
		return applicationService.getActiveApps(from, limit);
	}
	
	@RequestMapping(value = "/realtor/get_meetings.do", method = RequestMethod.POST)
	public @ResponseBody List<Meeting> getMeetings(@RequestParam("applicationId") long appId) {
		return meetingService.getMeetings(appId);
	}
	
	@RequestMapping(value = "/realtor/get_flats_for_meeting.do", method = RequestMethod.POST)
	public @ResponseBody List<SuitableOption> getFlatsForMeeting(@RequestParam("applicationId") long appId) {
		return applicationService.getOptions(appId);		
	}
	
	@RequestMapping(value = "/realtor/create_meeting.do", method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> addMeeting(Meeting meeting) {
		return meetingService.create(meeting);
	}
	
	@RequestMapping(value = "/realtor/delete_meeting.do", method = RequestMethod.POST)
	public @ResponseBody boolean deleteMeeting(Meeting meeting) {
		return meetingService.delete(meeting);
	}
	
	@RequestMapping(value = "/realtor/finish_meeting.do", method = RequestMethod.POST)
	public @ResponseBody boolean finishMeeting(Meeting meeting) {
		return meetingService.finish(meeting);
	}
	
	@RequestMapping(value = "/realtor/get_application_details.do", method = RequestMethod.POST)
	public @ResponseBody SaleApplication getSaleApplication(@RequestParam("optionsId") long optionsId) {
		return applicationService.getSaleAppByOption(optionsId);
	}
	
	@RequestMapping(value = "/realtor/get_meetings_by_date.do", method = RequestMethod.POST)
	public @ResponseBody List<Meeting> getMeetings(@DateTimeFormat(pattern = "MM.dd.yyyy") Date date) {
		return null;
	}
	
	@Autowired
	public void setMeetingService(MeetingService meetingService) {
		this.meetingService = meetingService;
	}
	
	@Autowired
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}
}
